﻿@model CategoryModel

@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)

<div id="category-edit">
    <ul>
        <li @Html.RenderSelectedTabIndex(0, GetSelectedTabIndex())>
            @T("Admin.Catalog.Categories.Info")
        </li>
        <li @Html.RenderSelectedTabIndex(1, GetSelectedTabIndex())>
            @T("Admin.Common.SEO")
        </li>
        <li @Html.RenderSelectedTabIndex(2, GetSelectedTabIndex())>
            @T("Admin.Catalog.Categories.Products")
        </li>
        <li @Html.RenderSelectedTabIndex(3, GetSelectedTabIndex())>
            @T("Admin.Catalog.Categories.Discounts")
        </li>
        <li @Html.RenderSelectedTabIndex(4, GetSelectedTabIndex())>
            @T("Admin.Catalog.Categories.Acl")
        </li>
        <li @Html.RenderSelectedTabIndex(5, GetSelectedTabIndex())>
            @T("Admin.Catalog.Categories.Stores")
        </li>
    </ul>
    <div>
        @TabInfo()
    </div>
    <div>
        @TabSeo()
    </div>
    <div>
        @TabProducts()
    </div>
    <div>
        @TabDiscounts()
    </div>
    <div>
        @TabAcl()
    </div>
    <div>
        @TabStores()
    </div>
</div>
<script>
    $(document).ready(function() {
        $("#category-edit").kendoTabStrip({
            animation:  {
                open: {
                    effects: "fadeIn"
                }
            },
            select: tabstrip_on_tab_select
        });
    });
</script>

@{
    //custom tabs
    var eventMessage = new AdminTabStripCreated(this.Html, "category-edit");
    EngineContext.Current.Resolve<IEventPublisher>().Publish(eventMessage);
    foreach (var eventBlock in eventMessage.BlocksToRender)
    {
        @eventBlock
    }
}


@*save selected tab index*@
<input type="hidden" id="selected-tab-index" name="selected-tab-index" value="@(GetSelectedTabIndex())">

@helper TabInfo()
    {
    
    <script type="text/javascript">
        $(document).ready(function () {
            $("#@Html.FieldIdFor(model => model.AllowCustomersToSelectPageSize)").click(togglePageSize);
            togglePageSize();
        });

        function togglePageSize() {
            if ($('#@Html.FieldIdFor(model => model.AllowCustomersToSelectPageSize)').is(':checked')) {
                $('#pnlPageSize').hide();
                $('#pnlPageSizeOptions').show();
            } else {
                $('#pnlPageSize').show();
                $('#pnlPageSizeOptions').hide();
            }
        }
    </script>
        
    @(Html.LocalizedEditor<CategoryModel, CategoryLocalizedModel>("category-info-localized",
                @<table class="adminContent">
                    <tr>
                        <td class="adminTitle">
                            @Html.NopLabelFor(model => model.Locales[item].Name):
                        </td>
                        <td class="adminData">
                            @Html.EditorFor(model => model.Locales[item].Name)
                            @Html.ValidationMessageFor(model => model.Locales[item].Name)
                        </td>
                    </tr>
                    <tr>
                        <td class="adminTitle">
                            @Html.NopLabelFor(model => model.Locales[item].Description):
                        </td>
                        <td class="adminData">
                            @Html.EditorFor(model => model.Locales[item].Description, "RichEditor")
                            @Html.ValidationMessageFor(model => model.Locales[item].Description)
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            @Html.HiddenFor(model => model.Locales[item].LanguageId)
                        </td>
                    </tr>
                </table>
                ,
                @<table class="adminContent">
                    <tr>
                        <td class="adminTitle">
                            @Html.NopLabelFor(model => model.Name):
                        </td>
                        <td class="adminData">
                            @Html.EditorFor(model => model.Name)
                            @Html.ValidationMessageFor(model => model.Name)
                        </td>
                    </tr>
                    <tr>
                        <td class="adminTitle">
                            @Html.NopLabelFor(model => model.Description):
                        </td>
                        <td class="adminData">
                            @Html.EditorFor(x => x.Description, "RichEditor")
                            @Html.ValidationMessageFor(model => model.Description)
                        </td>
                    </tr>
                </table>
            ))
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CategoryTemplateId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.CategoryTemplateId, Model.AvailableCategoryTemplates)
                @Html.ValidationMessageFor(model => model.CategoryTemplateId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.PictureId)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.PictureId)
                @Html.ValidationMessageFor(model => model.PictureId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.ParentCategoryId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.ParentCategoryId, Model.AvailableCategories)
                @Html.ValidationMessageFor(model => model.ParentCategoryId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.PriceRanges):
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.PriceRanges)
                @Html.ValidationMessageFor(model => model.PriceRanges)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.ShowOnHomePage):
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.ShowOnHomePage)
                @Html.ValidationMessageFor(model => model.ShowOnHomePage)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.IncludeInTopMenu):
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.IncludeInTopMenu)
                @Html.ValidationMessageFor(model => model.IncludeInTopMenu)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AllowCustomersToSelectPageSize):
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.AllowCustomersToSelectPageSize)
                @Html.ValidationMessageFor(model => model.AllowCustomersToSelectPageSize)
            </td>
        </tr>
        <tr id="pnlPageSize">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.PageSize):
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.PageSize)
                @Html.ValidationMessageFor(model => model.PageSize)
            </td>
        </tr>
        <tr id="pnlPageSizeOptions">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.PageSizeOptions):
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.PageSizeOptions)
                @Html.ValidationMessageFor(model => model.PageSizeOptions)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Published):
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.Published)
                @Html.ValidationMessageFor(model => model.Published)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.DisplayOrder):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.DisplayOrder)
                @Html.ValidationMessageFor(model => model.DisplayOrder)
            </td>
        </tr>
    </table>
}
@helper TabSeo()
    {
    @(Html.LocalizedEditor<CategoryModel, CategoryLocalizedModel>("category-seo-localized",
               @<table class="adminContent">
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.Locales[item].MetaKeywords):
                       </td>
                       <td class="adminData">
                           @Html.EditorFor(model => model.Locales[item].MetaKeywords)
                           @Html.ValidationMessageFor(model => model.Locales[item].MetaKeywords)
                       </td>
                   </tr>
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.Locales[item].MetaDescription):
                       </td>
                       <td class="adminData">
                           @Html.TextAreaFor(model => model.Locales[item].MetaDescription)
                           @Html.ValidationMessageFor(model => model.Locales[item].MetaDescription)
                       </td>
                   </tr>
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.Locales[item].MetaTitle):
                       </td>
                       <td class="adminData">
                           @Html.EditorFor(model => model.Locales[item].MetaTitle)
                           @Html.ValidationMessageFor(model => model.Locales[item].MetaTitle)
                       </td>
                   </tr>
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.Locales[item].SeName):
                       </td>
                       <td class="adminData">
                           @Html.EditorFor(model => model.Locales[item].SeName)
                           @Html.ValidationMessageFor(model => model.Locales[item].SeName)
                       </td>
                   </tr>
                   <tr>
                       <td colspan="2">
                           @Html.HiddenFor(model => model.Locales[item].LanguageId)
                       </td>
                   </tr>
               </table>
               ,
               @<table class="adminContent">
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.MetaKeywords):
                       </td>
                       <td class="adminData">
                           @Html.EditorFor(x => x.MetaKeywords)
                           @Html.ValidationMessageFor(model => model.MetaKeywords)
                       </td>
                   </tr>
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.MetaDescription):
                       </td>
                       <td class="adminData">
                           @Html.TextAreaFor(x => x.MetaDescription)
                           @Html.ValidationMessageFor(model => model.MetaDescription)
                       </td>
                   </tr>
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.MetaTitle):
                       </td>
                       <td class="adminData">
                           @Html.EditorFor(x => x.MetaTitle)
                           @Html.ValidationMessageFor(model => model.MetaTitle)
                       </td>
                   </tr>
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.SeName):
                       </td>
                       <td class="adminData">
                           @Html.EditorFor(x => x.SeName)
                           @Html.ValidationMessageFor(model => model.SeName)
                       </td>
                   </tr>
               </table>))
}
@helper TabProducts()
    {
    if (Model.Id > 0)
    {
        var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
            <table class="adminContent">
                <tr>
                    <td>

                        <div id="products-grid"></div>

                        <script>
                            $(document).ready(function () {
                                $("#products-grid").kendoGrid({
                                    dataSource: {
                                        type: "json",
                                        transport: {
                                            read: {
                                                url: "@Html.Raw(Url.Action("ProductList", "Category", new { categoryId = Model.Id }))",
                                                type: "POST",
                                                dataType: "json"
                                            },
                                            update: {
                                                url:"@Html.Raw(Url.Action("ProductUpdate", "Category"))",
                                                type: "POST",
                                                dataType: "json"
                                            },
                                            destroy: {
                                                url: "@Html.Raw(Url.Action("ProductDelete", "Category"))",
                                                type: "POST",
                                                dataType: "json"
                                            }
                                        },
                                        schema: {
                                            data: "Data",
                                            total: "Total",
                                            errors: "Errors",
                                            model: {
                                                id: "Id",
                                                fields: {
                                                    ProductName: {editable: false, type: "string" },
                                                    IsFeaturedProduct: {editable: true, type: "boolean" },
                                                    DisplayOrder1: {editable: true, type: "number" },
                                                    ProductId: {editable: false, type: "number" }
                                                }
                                            }
                                        },
                                        requestEnd: function(e) {
                                            if(e.type=="update") {
                                                this.read();
                                            }
                                        },
                                        error: function(e) {
                                            display_kendoui_grid_error(e);
                                            // Cancel the changes
                                            this.cancelChanges();
                                        },
                                        pageSize: @(gridPageSize),
                                        serverPaging: true,
                                        serverFiltering: true,
                                        serverSorting: true
                                    },
                                    pageable: {
                                        refresh: true
                                    },
                                    editable: {
                                        confirmation: false,
                                        mode: "inline"
                                    },
                                    scrollable: false,
                                    columns: [{
                                        field: "ProductName",
                                        title: "@T("Admin.Catalog.Categories.Products.Fields.Product")"
                                    }, {
                                        field: "IsFeaturedProduct",
                                        title: "@T("Admin.Catalog.Categories.Products.Fields.IsFeaturedProduct")",
                                        width: 150,
                                        headerAttributes: { style: "text-align:center" },
                                        attributes: { style: "text-align:center" },
                                        template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=IsFeaturedProduct#.gif" />'
                                    }, {
                                        field: "DisplayOrder1",
                                        title: "@T("Admin.Catalog.Categories.Products.Fields.DisplayOrder")",
                                        //integer format
                                        format: "{0:0}",
                                        width: 150
                                    }, {
                                        field: "ProductId",
                                        title: "@T("Admin.Common.View")",
                                        width: 100,
                                        template: '<a href="@Url.Content("~/Admin/Product/Edit/")#=ProductId#">@T("Admin.Common.View")</a>'
                                    }, {
                                        command: [{
                                            name: "edit", 
                                            text: "@T("Admin.Common.Edit")"
                                        }, {
                                            name: "destroy", 
                                            text: "@T("Admin.Common.Delete")"
                                        }],
                                        width: 200
                                    }]
                                });
                            });
                        </script>
                    </td>
                </tr>
                <tr>
                    <td width="100%">
                        <input type="submit" id="btnAddNewProduct" name="btnAddNewProduct" value="@T("Admin.Catalog.Categories.Products.AddNew")" onclick="javascript:OpenWindow('@(Url.Action("ProductAddPopup", "Category", new { categoryId = Model.Id, btnId = "btnRefreshProducts", formId = "category-form" }))', 800, 800, true); return false;" class="k-button" />
                        <input type="submit" id="btnRefreshProducts" name="btnRefreshProducts" style="display: none" />
                        <script type="text/javascript">
                            $(document).ready(function () {
                                $('#btnRefreshProducts').click(function () {
                                    //refresh grid
                                    var optionsGrid = $("#products-grid").data('kendoGrid');
                                    optionsGrid.dataSource.read();

                                    //return false to don't reload a page
                                    return false;
                                });
                            });
                        </script>
                    </td>
                </tr>
            </table>
    }
    else
    {
            @T("Admin.Catalog.Categories.Products.SaveBeforeEdit")
    }
}

@helper TabDiscounts()
    {
        if (Model.AvailableDiscounts != null && Model.AvailableDiscounts.Count > 0)
        {
            foreach (var discount in Model.AvailableDiscounts)
            {
            <input type="checkbox" name="SelectedDiscountIds" value="@discount.Id"
            @if (Model.SelectedDiscountIds != null && Model.SelectedDiscountIds.Contains(discount.Id))
            {
                <text>checked="checked"</text>
            }
            />@discount.Name
            <br />
            }
        }
        else
        {
            @T("Admin.Catalog.Categories.Discounts.NoDiscounts")
        }
}
@helper TabAcl()
{
    
    <script type="text/javascript">
        $(document).ready(function () {

            $("#@Html.FieldIdFor(model => model.SubjectToAcl)").click(toggleAcl);
        
            toggleAcl();
        });


        function toggleAcl() {
            if ($('#@Html.FieldIdFor(model => model.SubjectToAcl)').is(':checked')) {
                $('#pnl-acl').show();
            }
            else {
                $('#pnl-acl').hide();
            }
        }

    </script>
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SubjectToAcl):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SubjectToAcl)
                @Html.ValidationMessageFor(model => model.SubjectToAcl)
            </td>
        </tr>
        <tr id="pnl-acl">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AvailableCustomerRoles):
            </td>
            <td class="adminData">
                @if (Model.AvailableCustomerRoles != null && Model.AvailableCustomerRoles.Count > 0)
                {
                    foreach (var customerRole in Model.AvailableCustomerRoles)
                    {
                    <div>
                        <input type="checkbox" name="SelectedCustomerRoleIds" value="@customerRole.Id" checked="@(Model.SelectedCustomerRoleIds != null && Model.SelectedCustomerRoleIds.Contains(customerRole.Id))" />@customerRole.Name
                    </div>
                    }
                }
                else
                {
                    <div>No customer roles defined</div>
                }</td>
        </tr>
    </table>
}

@helper TabStores()
{
    <script type="text/javascript">
        $(document).ready(function () {

            $("#@Html.FieldIdFor(model => model.LimitedToStores)").click(toggleStoreMapping);
        
            toggleStoreMapping();
        });


        function toggleStoreMapping() {
            if ($('#@Html.FieldIdFor(model => model.LimitedToStores)').is(':checked')) {
                $('#pnl-available-stores').show();
            }
            else {
                $('#pnl-available-stores').hide();
            }
        }

    </script>
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.LimitedToStores):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.LimitedToStores)
                @Html.ValidationMessageFor(model => model.LimitedToStores)
            </td>
        </tr>
        <tr id="pnl-available-stores">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AvailableStores):
            </td>
            <td class="adminData">
                @if (Model.AvailableStores != null && Model.AvailableStores.Count > 0)
                {
                    foreach (var store in Model.AvailableStores)
                    {
                    <div>
                        <input type="checkbox" name="SelectedStoreIds" value="@store.Id" checked="@(Model.SelectedStoreIds != null && Model.SelectedStoreIds.Contains(store.Id))" />@store.Name
                    </div>
                    }
                }
                else
                {
                    <div>No stores defined</div>
                }</td>
        </tr>
    </table>
}
